package com.itheima.reggie_auto.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie_auto.common.R;
import com.itheima.reggie_auto.entity.Category;
import com.itheima.reggie_auto.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author 刘少宽
 * @since 2022-09-23
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 分类分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {
        log.info("分类分页查询：page = {},pageSize = {}",page,pageSize);
        Page<Category> pageInfo = categoryService.page(page,pageSize);
        return R.success(pageInfo);
    }

    /**
     * 新增分类数据
     * @param category
     * @return
     */
     @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("新增分类数据：{}",category);

        categoryService.save(category);
        return R.success("保存分类信息成功");
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteById(Long id) {
         categoryService.deleteById(id);
         return R.success("删除成功");
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        categoryService.update(category);
        return R.success("修改分类成功");
    }
    @GetMapping("/list")
    public R<List<Category>> getList(Category category){
        List<Category> list = categoryService.getList(category);
        return R.success(list);
    }

}

